class ClientsController < ApplicationController
  protect_from_forgery :only => [:create, :destroy, :update]

  def index
    if params[:search]
      @clients = Client.paginate :per_page => 10, :page => params[:page], :order => 'surname',
                                 :conditions => ['surname LIKE ? || name LIKE ? || society LIKE ?', 
                                    "%#{params[:search]}%", "%#{params[:search]}%", "%#{params[:search]}%"], 
                                 :order => 'surname'
    else
      @clients = Client.paginate :per_page => 10, :page => params[:page], :order => 'surname'
    end

    respond_to do |format|
      format.html
      format.xml  { render :xml => @clients }
    end
  end
  
  def topten
    @clients = Client.find_all_by_topten(1, :order => 'surname')

    respond_to do |format|
      format.html
      format.xml  { render :xml => @clients }
    end
  end

  def show
    @client = Client.find(params[:id])
    @contracts = Contract.find_all_by_client_id(params[:id])

    respond_to do |format|
      format.html
      format.xml  { render :xml => @client }
    end
  end
  
  def new
    @client = Client.new
    
    respond_to do |format|
      format.html
      format.xml  { render :xml => @client }
    end
  end

  def edit
    @client = Client.find(params[:id])
  end

  def create
    @client = Client.new(params[:client])
    
    respond_to do |format|
      if @client.save
        flash[:notice] = 'Cliente creato con successo.'
        format.html { redirect_to(@client) }
        format.xml  { render :xml => @client, :status => :created, :location => @client }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @client.errors, :status => :unprocessable_entity }
      end
    end
  end

  def update
    @client = Client.find(params[:id])

    respond_to do |format|
      if @client.update_attributes(params[:client])
        flash[:notice] = 'Cliente aggiornato con successo.'
        format.html { redirect_to(@client) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @client.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @client = Client.find(params[:id])
    @client.destroy

    respond_to do |format|
      format.html { redirect_to(clients_url) }
      format.xml  { head :ok }
    end
  end
end
